<?php
class Reward extends Model_Abstract
{
		
	//invitation_id, amt is only for peanutlabs
	public function addRewardByMonetization($uid, $name, $credit, $transactionId=null, $invitationId=null, $amt=null){
		$now = time();
		try
		{
			$sql = "insert into rewards set user_id=?, name=?, credit=?, created=?, created_date=?, transaction_id=?, invitation_id=?, amt=?";
			$reult = $this->_connection->execute($sql, array($uid, $name, $credit, $now, date('YmdHis', $now), $transactionId, $invitationId, $amt));
			return $reult;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
		
	public function exists($uid, $pid)
	{
		try
		{
			$sql = "select * from rewards where user_id=? and transaction_id=? limit 1";
			$result = $this->_connection->query($sql, array($uid, $pid));
			if (count($result) > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function getRewardsToday($uid){
		$timeStarted = TimeUtil::startOfDay();
		try{
			$sql = "select name, credit, created from rewards where user_id=? and created>$timeStarted";
			$reult = $this->_connection->query($sql, array($uid));
			if(count($reult) == 0){
				return false;
			}else{
				return $reult;
			}
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	
	public function getRewardsWeekly($uid){
		$timeStarted = TimeUtil::startOfWeek();
		try{
			$sql = "select name, credit, created from rewards where user_id=? and created>$timeStarted";
			$reult = $this->_connection->query($sql, array($uid));
			if(count($reult) == 0){
				return false;
			}else{
				return $reult;
			}
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function getRewardsMonthly($uid){
		$timeStarted = TimeUtil::startOfMonth();
		try{
			$sql = "select name, credit, created from rewards where user_id=? and created>$timeStarted";
			$reult = $this->_connection->query($sql, array($uid));
			if(count($reult) == 0){
				return false;
			}else{
				return $reult;
			}
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function getRewardsAll($uid){
		try{
			$sql = 'select name, credit, created from rewards where user_id=? order by created desc';
			return $this->_connection->query($sql, array($uid));
			
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function getUserRewards($uid){
		try{
			$sql = ' SELECT SUM(credit) AS credits FROM rewards WHERE user_id=? ';
			$result = $this->_connection->query($sql, array($uid));
			if(count($result) > 0){
				return $result[0]['credits'];
			}
			return false;
			
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
		
}